package com.web.my12306.controller;

import com.alibaba.fastjson.JSON;
import com.web.my12306.pojo.EasyUIDataGridJSON;
import com.web.my12306.pojo.Train;
import com.web.my12306.service.TrainService;
import com.web.my12306.service.impl.TrainServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TrainServlet extends InnerDispatcherController {
	private TrainService trainService = new TrainServiceImpl();

	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if ("POST".equals(request.getMethod())) {

			Train train = new Train();
			String trainno = request.getParameter("trainno");
			train.setTrainno(trainno);

			String dtime = request.getParameter("departure_time");
			Timestamp departure_time = null;
			try {
				departure_time = Timestamp.valueOf(dtime);
			} catch (Exception e) {
				e.printStackTrace();
			}
			train.setDeparture_Time(departure_time);

			String atime = request.getParameter("arrival_time");
			Timestamp arrival_time = null;
			try {
				arrival_time = Timestamp.valueOf(atime);
			} catch (Exception e) {
				e.printStackTrace();
			}
			train.setArrival_Time(arrival_time);

			String dstation = request.getParameter("dstation");
			train.setDstation(dstation);

			String terminal = request.getParameter("terminal");
			train.setTerminal(terminal);

			String pricestr = request.getParameter("price");
			double price = 0;
			try {
				price = Double.valueOf(pricestr);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			train.setPrice(price);

			String carriagestr = request.getParameter("carriage");
			int carriage = 0;
			try {
				carriage = Integer.valueOf(carriagestr);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}

			train.setState(1);

			trainService.add(train, carriage);
			// 跳转界面保留
			response.getWriter().print("添加列车成功");
		} else {
			request.getRequestDispatcher("/error.html").forward(request, response);
		}

	}

	public void cancelTrain(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if ("POST".equals(request.getMethod())) {
			String tidstr = request.getParameter("tid");
			int tid = Integer.valueOf(tidstr);
			trainService.cancelByTid(tid);
		} else {
			request.getRequestDispatcher("/error.html").forward(request, response);
		}

	}


	public List<Train> getTrains() {
		List<Train> trains = new ArrayList<Train>();
		trains = trainService.getTrainAll();
		
		return trains;
		}
	/**
	 * 将数据库查询结果传到jsp中
	 * @author 龚丹丹
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void pageGatagrid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		
		System.out.println("page:" + page + ";rows:" + rows);
		
		int start = 0;
		if (page == 2) {
			start = 10;
		}
		
		List<Train> data = this.getTrains();
		
		List<Train> trains = new ArrayList<>();
		for (int i = start ; i < trains.size() ; i++) {
			trains.add(data.get(i));
		}
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(data.size());
		responseObject.setRows(data);
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setContentType("application/json");
        response.getWriter().append(JSON.toJSONString(responseObject));
		System.out.println(JSON.toJSONString(responseObject));
	}
	
	
	public void getTrainBySelect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String dstation=(String)request.getParameter("dstation");
        String terminal=(String)request.getParameter("terminal");
//        String departure_Time=(String)request.getParameter("departure_Time");
        List<Train> traincheck = new ArrayList<Train>();
        traincheck = trainService.getTrainBySelect(dstation, terminal);
        if(traincheck.size()>0) {
        	request.setAttribute("traincheck", traincheck);
        	request.getRequestDispatcher("/showTrain.jsp").forward(request, response);
        }else {
        	response.getWriter().print("没有符合条件的车次");
        }
	}

}
